﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;
using System.Drawing;

public partial class Mod_InitFile_frm_Provide : System.Web.UI.Page
{
    private void ControlButtonVisible(string UserLogID, string CurrPageID)
    {
        ClsMenuDAL cls_Menu = new ClsMenuDAL();
        DataSet dst = cls_Menu.GetCurrUserButtonPopedom(UserLogID, CurrPageID);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            int intCount = dst.Tables[0].Rows.Count - 1;

            for (int i = intCount; i >= 0; i--)
            {
                int intButtonID = Int32.Parse(dst.Tables[0].Rows[i][2].ToString());

                switch (intButtonID)
                {
                    case 0:
                        this.panAdd.Visible = true;
                        break;
                    case 1:
                        this.hidButtonEdit.Value = intButtonID.ToString();
                        break;
                    case 2:
                        this.panDelete.Visible = true;
                        break;
                    case 3:
                        this.lbtnDelete1.Enabled = true;
                        break;
                    case 4:
                        this.panAudit.Visible = true;
                        break;
                    case 5:
                        this.hidButtonOAudit.Value = intButtonID.ToString();
                        break;
                    case 6:
                        this.panRef.Visible = true;
                        break;
                    case 7:
                        this.panSearch.Visible = true;
                        break;
                }
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            if (!IsPostBack)
            {
                ////////////////按钮权限控制 Begin /////////////////////////
                string strCurrPageID = this.Page.Request.Params["PageID"];
                this.ControlButtonVisible(strUserID, strCurrPageID);
                //////////////////////  End ///////////////////////////////

                this.GetProvides();
            }
            else
            {
                this.lblJs.Text = "";//清空JS脚本
            }

            this.ibtnDelete0.Attributes.Add("onClick", "return confirm('您真的要作废所选的项吗？');");
            this.lbtnDelete0.Attributes.Add("onClick", "return confirm('您真的要作废所选的项吗？');");
            this.lbtnDelete1.Attributes.Add("onClick", "return confirm('彻底作废将无法恢复数据。您真的要作废所选的项吗？');");
        }
        else
        {
            ClsMenuDAL cls_Menu = new ClsMenuDAL();
            string strMessage = cls_Menu.GetSystemCode("1", "00", this.labId.Text.Trim(), 25);//取消当前系统编码
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    public void ChangPage(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdResult.CurrentPageIndex;
            int intPageCounts = this.dgrdResult.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindProvideByDataView(dst, intPageIndex);
        }
    }

    public void AddProvideInfo(object sender, EventArgs e)
    {
        
        ClsSellFillDAL cls_ProvideSet = new ClsSellFillDAL();
        
        string strCt_ID = this.labId.Text.Trim();
        string strCt_Name = this.txt2.Text.Trim();
        string money = this.TextBox1.Text.Trim();
        string moneyy = this.TextBox2.Text.Trim();
        string date = this.date.Text.Trim();
        string strCt_Addr = this.txtAddr.Text.Trim();
        string strCt_Linkman = this.txtLinkman.Text.Trim();
        string strCt_Phone = this.txtPhone.Text.Trim();
        string strCt_Dept = this.txtDept.Text.Trim();
        int iCt_state = int.Parse(this.drpState.SelectedValue);
        string strCt_Remark = this.txtRemark.Text.Trim();
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        string strUserIDD = cls_Property.SetOrGetUserID;
        if (strUserIDD != "")
        {
            Boolean bolIsCussee = cls_ProvideSet.AddProvideSet(strCt_ID, strCt_Name, strCt_Addr, strCt_Linkman, strCt_Phone, strCt_Dept,
                iCt_state, money, moneyy, date, strCt_Remark, strUserIDD);


            if (bolIsCussee)
            {
                this.GetProvides();//重新绑定
                ClsMenuDAL cls_Menu = new ClsMenuDAL();
                string strCodeId = cls_Menu.GetSystemCode("0", "00", "", 25);
                this.labId.Text = strCodeId;
                StringBuilder strJs = new StringBuilder();
                //strJs.Append("<script>document.getElementById('trAdd').style.display='block';");
                //strJs.Append("var dt=new Date();var y=dt.getYear();var m=dt.getMonth();var d=dt.getDay();var h=dt.getHours();var mi=dt.getMinutes();var s=dt.getSeconds();var ct=y+''+m+''+d+''+h+''+mi+''+s;");
                //strJs.Append("document.getElementById('txt1').value = 'C_'+ct;");
                //strJs.Append("document.getElementById('txt2').value = '';");
                //strJs.Append("document.getElementById('txtAddr').value = '';");
                //strJs.Append("document.getElementById('txtLinkman').value = '';");
                //strJs.Append("document.getElementById('txtPhone').value = '';");
                //strJs.Append("document.getElementById('txtDept').value = '';");
                //strJs.Append("document.getElementById('txtRemark').value = '';");
                //strJs.Append("document.getElementById('" + this.txt2.ClientID + "').select();");
                //strJs.Append("document.getElementById('" + this.txt2.ClientID + "').focus()</script>";            
                this.lblJs.Text = "<script>document.getElementById('" + this.txt2.ClientID + "').select();document.getElementById('" + this.txt2.ClientID + "').focus()</script>";
            }
            else
            {
                this.lblJs.Text = "<script>alert('新增供商信息失败，请重试！')</script>";
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    public void UpDateProvideInfo(object sender, EventArgs e)
    {
        ClsSellFillDAL cls_ProvideSet = new ClsSellFillDAL();
        ImageButton ibtn = (ImageButton)sender;
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        string strUserIDD = cls_Property.SetOrGetUserID;
        int intCommandName = Int32.Parse(ibtn.CommandName);
        if (strUserIDD != "")
        {
            if (intCommandName == 1)//确认
            {
                int intCount = this.dgrdResult.Items.Count;

                for (int i = 0; i < intCount; i++)
                {
                    CheckBox chkBox = (CheckBox)this.dgrdResult.Items[i].FindControl("chkSelect");

                    if (chkBox.Checked)
                    {
                        TextBox txtBox = (TextBox)this.dgrdResult.Items[i].FindControl("Ct_id");

                        Boolean bolIsCussee = cls_ProvideSet.AuditStatProvideSet(txtBox.Text.Trim(), intCommandName, strUserIDD);

                        if (!bolIsCussee)
                        {
                            this.lblJs.Text = "<script>alert('确认供商信息失败，请重试！')</script>";
                        }
                    }
                }

                this.GetProvides();//重新绑定
            }
            else//取消确认，编辑，取消编辑，保存编辑
            {
                int intIndex = Int32.Parse(ibtn.CommandArgument);

                CheckBox chk = (CheckBox)this.dgrdResult.Items[intIndex].FindControl("chkSelect");
                TextBox ct_id = (TextBox)this.dgrdResult.Items[intIndex].FindControl("ct_id");
                CuTextSelectBox ct_name = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ct_name");
                CuTextSelectBox ct_addr = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ct_addr");
                TextBox ct_contactor = (TextBox)this.dgrdResult.Items[intIndex].FindControl("ct_contactor");
                TextBox ct_tel = (TextBox)this.dgrdResult.Items[intIndex].FindControl("ct_tel");
                TextBox TextBox1 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("TextBox1");
                TextBox TextBox2 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("TextBox2");
                CuTextSelectBox ct_dept = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ct_dept");

                TextBox txtState = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtState");
                DropDownList drpState = (DropDownList)this.dgrdResult.Items[intIndex].FindControl("drpState");

                TextBox remark = (TextBox)this.dgrdResult.Items[intIndex].FindControl("remark");
                ImageButton ibtnEdit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnEdit");
                ImageButton ibtnCan = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnCan");
                ImageButton ibtnSer = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnSer");
                ImageButton ibtnOAudit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnOAudit");


                if (intCommandName == 0)//取消确认
                {
                    Boolean bolIsCussee = cls_ProvideSet.AuditStatProvideSet(ct_id.Text.Trim(), intCommandName, strUserIDD);

                    if (bolIsCussee)
                    {
                        this.GetProvides();//重新绑定
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('取消确认供商信息失败，请重试！')</script>";
                    }
                }
                else if (intCommandName == 2)//编辑
                {
                    chk.Visible = ibtnEdit.Visible = txtState.Visible = false;
                    ibtnCan.Visible = ibtnSer.Visible = drpState.Visible = true;
                    TextBox1.ReadOnly = TextBox2.ReadOnly = ct_name.ReadOnly = ct_addr.ReadOnly = ct_contactor.ReadOnly = ct_tel.ReadOnly = ct_dept.ReadOnly = remark.ReadOnly = false;
                    TextBox1.BorderWidth = TextBox2.BorderWidth = ct_contactor.BorderWidth = ct_tel.BorderWidth = remark.BorderWidth = 1;
                    //ct_name.BorderWidth =ct_name.BorderWidth =
                    //让第一个文本框获取焦点
                    this.lblJs.Text = "<script>document.getElementById('" + ct_name.ClientID + "').select();document.getElementById('" + ct_name.ClientID + "').focus()</script>";
                }
                else if (intCommandName == 3)//取消编辑
                {
                    chk.Visible = ibtnEdit.Visible = txtState.Visible = true;
                    ibtnCan.Visible = ibtnSer.Visible = drpState.Visible = false;
                    TextBox1.ReadOnly = TextBox2.ReadOnly = ct_name.ReadOnly = remark.ReadOnly = ct_addr.ReadOnly = ct_contactor.ReadOnly = ct_tel.ReadOnly = ct_dept.ReadOnly = true;
                    TextBox1.BorderWidth = TextBox2.BorderWidth = remark.BorderWidth = ct_contactor.BorderWidth = ct_tel.BorderWidth = 0;

                    this.lblJs.Text = "";
                    this.GetProvides();//重新绑定
                }
                else//保存编辑
                {
                    Boolean bolIsCussee = cls_ProvideSet.UpDateProvideSet(ct_id.Text.Trim(), ct_name.Text.Trim(), ct_addr.Text.Trim(), ct_contactor.Text.Trim(),
                                ct_tel.Text.Trim(), ct_dept.Text.Trim(), int.Parse(drpState.SelectedValue), TextBox1.Text.Trim(), TextBox2.Text.Trim(), remark.Text.Trim());
                    if (bolIsCussee)
                    {
                        this.lblJs.Text = "";
                        this.GetProvides();//重新绑定
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('修改供商信息失败，请重试！')</script>";
                    }
                }
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    public void lbtnDelete_Click(object sender, EventArgs e)
    {
        int intCommandName = Int32.Parse(((LinkButton)sender).CommandName);

        this.DeleteProvideInfo(intCommandName);
    }

    private void DeleteProvideInfo(int CommandName)
    {
        ClsSellFillDAL cls_ProvideSet = new ClsSellFillDAL();

        if (CommandName == 1 || CommandName == -1)
        {
            int intCount = this.dgrdResult.Items.Count;

            for (int i = 0; i < intCount; i++)
            {
                CheckBox chkBox = (CheckBox)this.dgrdResult.Items[i].FindControl("chkSelect");

                if (chkBox.Checked)
                {
                    TextBox txtBox = (TextBox)this.dgrdResult.Items[i].FindControl("ct_id");

                    Boolean bolIsCussee = cls_ProvideSet.DeleteProvideSet(txtBox.Text.Trim(), CommandName);

                    if (!bolIsCussee)
                    {
                        this.lblJs.Text = "<script>alert('作废供商信息失败，请重试！')</script>";
                    }
                }
            }
        }

        this.GetProvides();//重新绑定
    }

    private void GetProvides()
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ClsSellFillDAL cls_ProvideSet = new ClsSellFillDAL();
            DataSet dst = cls_ProvideSet.GetProvideSets("00", strUserID, "DS000402");

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                cls_Property.SetOrGetCorporationDataSet = dst;

                this.BindProvideByDataView(dst, 0);
            }
            else
            {
                this.lblRsCounts.Text = "共0条记录";
                this.lblPageCounts.Text = "共0页";
                this.lblCurrPage.Text = "当前第0页";
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = false;
                this.lblRsCounts.ForeColor = this.lblPageCounts.ForeColor = this.lblCurrPage.ForeColor = this.lblGoTxt1.ForeColor = this.lblGoTxt2.ForeColor = Color.FromArgb(172, 168, 153);
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='index.aspx'</script>";
        }
    }

    private void BindProvideByDataView(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdResult.DataSource = dst;
            this.dgrdResult.DataBind();

            this.lblRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblPageCounts.Text = "共" + this.dgrdResult.PageCount + "页";
            this.lblCurrPage.Text = "当前第" + (this.dgrdResult.CurrentPageIndex + 1) + "页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = true;
            this.lblRsCounts.ForeColor = this.lblPageCounts.ForeColor = this.lblCurrPage.ForeColor = this.lblGoTxt1.ForeColor = this.lblGoTxt2.ForeColor = Color.FromArgb(78, 78, 78);

            if (this.dgrdResult.PageCount == 1)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = false;
                this.lblGoTxt1.ForeColor = this.lblGoTxt2.ForeColor = Color.FromArgb(172, 168, 153);
            }
            else if (this.dgrdResult.CurrentPageIndex == this.dgrdResult.PageCount - 1)
            {
                this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == 0)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = false;
            }
        }
    }

    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {
        string strKey = this.txtKey.Text.Trim();

        if (strKey != "" && strKey != "查询关键字")
        {
            ClsSellFillDAL cls_ProvideSet = new ClsSellFillDAL();
            DataSet dst = cls_ProvideSet.SearchProvideSets(strKey, 1, 1);

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                ClsPropertyDAL cls_Property = new ClsPropertyDAL();
                cls_Property.SetOrGetCorporationDataSet = dst;

                this.BindProvideByDataView(dst, 0);
            }
            else
            {
                this.lblJs.Text = "<script>alert('搜索结果为空！')</script>";
            }
        }
        else
        {
            this.lblJs.Text = "<script>alert('请输入搜索关键字！')</script>";
        }
    }

    protected void txtGo_TextChanged(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strIndex = this.txtGo.Text.Trim();
            this.txtGo.Text = String.Empty;

            if (strIndex != "")
            {
                try
                {
                    int intIndex = Int32.Parse(strIndex) - 1;

                    if (intIndex >= 0 && intIndex < this.dgrdResult.PageCount)
                    {
                        this.BindProvideByDataView(dst, intIndex);
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('请输入正确的页码数！')</script>";
                    }
                }
                catch
                {
                    this.lblJs.Text = "<script>alert('请输入数字类型的页码！')</script>";
                }
            }
            else
            {
                this.lblJs.Text = "<script>alert('请输入要跳转的页码！')</script>";
            }
        }
    }

    protected void dgrdResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Boolean bIs_State = Boolean.Parse(e.Item.Cells[1].Text.Trim());
            string strIs_audit = e.Item.Cells[2].Text.Trim();

            CheckBox chkbox = (CheckBox)e.Item.FindControl("chkSelect");
            TextBox remark = (TextBox)e.Item.FindControl("remark");
            ImageButton ibtnEdit = (ImageButton)e.Item.FindControl("ibtnEdit");
            ImageButton ibtnCan = (ImageButton)e.Item.FindControl("ibtnCan");
            ImageButton ibtnSer = (ImageButton)e.Item.FindControl("ibtnSer");
            ImageButton ibtnOAudit = (ImageButton)e.Item.FindControl("ibtnOAudit");
            System.Web.UI.WebControls.Image is_audit = (System.Web.UI.WebControls.Image)e.Item.FindControl("is_audit");

            TextBox txtState = (TextBox)e.Item.FindControl("txtState");
            DropDownList drpState = (DropDownList)e.Item.FindControl("drpState");

            drpState.SelectedIndex = (!bIs_State) ? 0 : 1;
            txtState.Text = txtState.ToolTip = drpState.SelectedItem.Text;

            //修改按回车提交
            if (remark.ReadOnly)
            {
                remark.Attributes.Add("onkeydown", "if(event.keyCode == 13){document.getElementById('" + ibtnSer.ClientID + "').click();}");
            }

            string strButtonEdit = this.hidButtonEdit.Value;
            string strButtonOAudit = this.hidButtonOAudit.Value;

            if (strIs_audit == "False")
            {
                if (strButtonEdit.Trim() == "1")
                {
                    if (this.panAudit.Visible || this.panDelete.Visible)
                    {
                        chkbox.Visible = true;
                    }

                    ibtnEdit.Visible = true;
                }
                else if (this.panAudit.Visible || this.panDelete.Visible)
                {
                    chkbox.Visible = true;
                }
                //chkbox.Visible = ibtnEdit.Visible = true;
                //ibtnOAudit.Visible = ibtnCan.Visible = ibtnSer.Visible = false;
                //is_audit.ImageUrl = "../App_Images/ico/6.gif";
            }
            else
            {
                ibtnOAudit.Visible = true;

                if (strButtonOAudit.Trim() != "5")
                {
                    ibtnOAudit.Enabled = false;
                    ibtnOAudit.ImageUrl = "../App_Images/ico/17.gif";
                }
                //ibtnOAudit.Visible = true;
                //chkbox.Visible = ibtnEdit.Visible = ibtnCan.Visible = ibtnSer.Visible = false;
                //is_audit.ImageUrl = "../App_Images/ico/7.gif";
            }
        }
    }

    protected void ibtnDelete0_Click(object sender, ImageClickEventArgs e)
    {
        int intCommandName = Int32.Parse(((ImageButton)sender).CommandName);

        this.DeleteProvideInfo(intCommandName);
    }

    protected void imgAdd_Click(object sender, ImageClickEventArgs e)
    {
        ClsMenuDAL cls_Menu = new ClsMenuDAL();

        string strCode = cls_Menu.GetSystemCode("0", "00", "", 25);

        if (strCode != "Error")
        {
            this.labId.Text = strCode;
            this.panAddRs.Visible = true;
        }
        else
        {
            this.lblJs.Text = "<script>alert('添加记录失败，请重试！')</script>";
        }
    }

    protected void imgCancel_Click(object sender, ImageClickEventArgs e)
    {
        this.panAddRs.Visible = false;

        ClsMenuDAL cls_Menu = new ClsMenuDAL();
        string strCode = cls_Menu.GetSystemCode("1", "00", this.labId.Text, 25);
    }
}
